angular.module("howMuch.services", ["ngResource"]);

angular.module('howMuch.services').factory("CustomerService", [
	'$resource',
	function($resource){
		return $resource(
			"/customers/:key",
			null,
			{
				"query": { method: "GET", isArray: false },
				"create": { method: "POST" },
				"update": { method: "PUT" },
	  			'delete': { method: "DELETE" }
			}
		);
	}
]);

angular.module('howMuch.services').factory("ProjectService", [
	'$resource',
	function($resource){
		return $resource(
			"/projects/:key",
			null,
			{
				"query": { method: "GET", isArray: false },
				"create": { method: "POST" },
				"update": { method: "PUT" },
	  			'delete': { method: "DELETE" }
			}
		);
	}
]);

angular.module('howMuch.services').factory("InvoiceService", [
	'$resource',
	function($resource){
		return $resource(
			"/invoices/:key",
			null,
			{
				"query": { method: "GET", isArray: false },
				"create": { method: "POST" },
				"update": { method: "PUT" },
	  			'delete': { method: "DELETE" }
			}
		);
	}
]);

angular.module('howMuch.services').factory('UserService', [
	'$resource',
	function($resource){
		return $resource(
			"/user", 
			null, 
			{
				"find": { method: "GET", isArray: false },
				"update": { method: "POST" }
			}
		);
	}
]);

angular.module('howMuch.services').factory('InvoiceSettingsService', [
	'$resource',
	function($resource){
		return $resource(
			"/invoice/settings",
			null,
			{
				"find": { method: "GET", isArray: false },
				"update": { method: "POST" }
			}
		);
	}
]);


/* Utility Service */
angular.module('howMuch.services').factory('HowMuchUtilityService', function(){
	return {
		/**
		* parameters: { type: "", title: "", message: "", isDismissable: true, code: 0 }
		*/
		createAlertMessage: function(parameters){
			var html = '<div class="alert alert-'+parameters.type+(parameters.isDismissable ? ' alert-dismissable' : '')+'" role="alert">';
			if(parameters.isDismissable){
				html += '<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>';
			}
			html += '<strong>'+parameters.title+' </strong> '+parameters.message;
			html += '</div>';

			return html;
		},
		cloneModel: function(model){
			var modelCopy = model.constructor();

			for(var attribute in model){
				if(model.hasOwnProperty(attribute)){
					modelCopy[attribute] = model[attribute];
				}
			}

			return modelCopy;
		},
		synchronizeModel: function(model, newModel){
			for(var attribute in newModel){
				if(model.hasOwnProperty(attribute)){
					model[attribute] = newModel[attribute];
				}
			}
		}
	};
});